﻿using System;
using System.Collections.Generic;
using System.IO;

class StudentsSerialization
{
    private const string DATA_FILENAME = @"..\..\students.bin";

    //public static void AddStudent(Student item, string outputFile)
    //{
    //    FileStream output = new FileStream(outputFile, FileMode.Append, FileAccess.Write);
    //    using (output)
    //    {
    //        BinaryFormatter formatter = new BinaryFormatter();
    //        formatter.Serialize(output, item);
    //    }
    //}

    //public static Student[] ReadAllStudents(string dataFile)
    //{
    //    List<Student> result = new List<Student>();
    //    FileStream input = new FileStream(dataFile, FileMode.Open, FileAccess.Read);
    //    using (input)
    //    {
    //        BinaryFormatter formatter = new BinaryFormatter();
    //        while (input.Position < input.Length)
    //        {
    //            result.Add((Student)formatter.Deserialize(input));
    //        }
    //    }
    //    return result.ToArray();
    //}

    //public static void Main()
    //{
    //    Student[] studs = new Student[3];
    //    studs[0] = new Student("Ivan", "Petkov", 1234);
    //    studs[1] = new Student("Pesho", "Goshov", 4567);
    //    studs[2] = new Student("Misho", "Vasilev", 8652);

    //    AddStudent(studs[0], DATA_FILENAME);
    //    AddStudent(studs[1], DATA_FILENAME);
    //    AddStudent(studs[2], DATA_FILENAME);

    //    Student[] result = ReadAllStudents(DATA_FILENAME);

    //    for (int i = 0; i < result.Length; i++)
    //    {
    //        Console.WriteLine("{0}, {1}, {2}",result[i].FirstName,result[i].LastName,result[i].FacultyNumber);
    //    }

    //}

    public static void AddStudent(Student entry, BinaryWriter outputStream)
    {
        outputStream.Write(entry.ToString());
    }

    public static Student[] ReadAllStudents(BinaryReader inputStream)
    {
        List<Student> result = new List<Student>();
        while (inputStream.BaseStream.Position < inputStream.BaseStream.Length)
        {
            string studentAsString = inputStream.ReadString();
            result.Add(Student.Parse(studentAsString));
        }
        return result.ToArray();
    }

    public static string DeleteStudent(BinaryReader inputStream, uint facNum)
    {
        List<Student> allStudents =new List<Student>( ReadAllStudents(inputStream));

        string tempFile = Path.GetTempFileName();
        FileStream outFile = new FileStream(tempFile, FileMode.Create, FileAccess.Write);
        BinaryWriter output = new BinaryWriter(outFile);

        for (int i = 0; i < allStudents.Count; i++)
        {
            if (allStudents[i].FacultyNumber == facNum)
            {
                allStudents.RemoveAt(i);
                i--;
            }
            else AddStudent(allStudents[i],output);
        }
        output.Close();
        outFile.Close();

        return tempFile;
    }

    public static void Main()
    {
        Student[] studs = new Student[3];
        studs[0] = new Student("Ivan", "Petkov", 1234);
        studs[1] = new Student("Pesho", "Goshov", 4567);
        studs[2] = new Student("Misho", "Vasilev", 8652);

        FileStream output = new FileStream(DATA_FILENAME, FileMode.Append, FileAccess.Write);
        using (output)
        {
            BinaryWriter outputStream = new BinaryWriter(output);
            using (outputStream)
            {
                AddStudent(studs[0], outputStream);
                AddStudent(studs[1], outputStream);
                AddStudent(studs[2], outputStream);
            }
        }

        //DeleteStudent(DATA_FILENAME,1234);
        Student[] result = ReadAlLStudents(DATA_FILENAME);

        for (int i = 0; i < result.Length; i++)
        {
            Console.WriteLine("{0}, {1}, {2}", result[i].FirstName, result[i].LastName, result[i].FacultyNumber);
        }

    }

    private static Student[] ReadAlLStudents(string inputFileName)
    {
        FileStream input = new FileStream(inputFileName, FileMode.Open, FileAccess.Read);
        Student[] result;
        using (input)
        {
            BinaryReader inputStream = new BinaryReader(input);
            using (inputStream)
            {
                result = ReadAllStudents(inputStream);
            }
        }
        return result;
    }

    private static void DeleteStudent(string dataFilename,uint facNum)
    {
        FileStream input = new FileStream(dataFilename, FileMode.Open, FileAccess.Read);
        string res;
        using (input)
        {
            BinaryReader inputStream = new BinaryReader(input);
            using (inputStream)
            {
                res = DeleteStudent(inputStream, facNum);
            }
        }
        File.Delete(dataFilename);
        File.Move(res, dataFilename);
        File.Delete(res);
    }

}
